package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.ActivityDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.ActivityInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.ActivityService;

@Service("activityService")
public class ActivityServiceImpl implements ActivityService {

	@Resource(name = "activityDao")
	private ActivityDao activityDao;

	@Override
	public ActivityInfo queryByPrimartKey(String cid) {
		return activityDao.getByKey(cid);
	}

	@Override
	public Pagination<ActivityInfo> queryPage(ActivityInfo activityInfo) {
		if (StringUtils.isNotEmpty(activityInfo.getOrderBy())
				&& activityInfo.getOrderType() != null) {
			if (activityInfo.getOrderType() == OrderType.asc) {
				activityInfo.setPage_orderByAsc("ac."
						+ activityInfo.getOrderBy());
			} else {
				activityInfo.setPage_orderByDesc("ac."
						+ activityInfo.getOrderBy());
			}
		}
		Integer count = activityDao.totalCount(activityInfo);
		activityInfo.calculate();
		List<ActivityInfo> list = activityDao.queryList(activityInfo);
		
		Pagination<ActivityInfo> page = activityDao.queryPagination(count,
				list, activityInfo);
		return page;
	}

	@Override
	public int save(ActivityInfo activityInfo, UserInfoVO currentUser) {
		activityInfo.setPublishTime(new Date());
		activityInfo.setUserId(currentUser.getUserId());
		activityInfo.setIsDelete(Constants.NOT_DELETED);
		if (StringUtils.isBlank(activityInfo.getIsLimitTime())) {
			activityInfo.setIsLimitTime("0");
		}
		if (StringUtils.isBlank(activityInfo.getIsTop())) {
			activityInfo.setIsTop("0");
		}
		activityInfo.setIsProgressing("1");
		return activityDao.insert(activityInfo);
	}

	@Override
	public int update(ActivityInfo activityInfo, UserInfoVO currentUser) {
		activityInfo.setUpdateTime(new Date());
		activityInfo.setUserId(currentUser.getUserId());
		return activityDao.update(activityInfo);
	}

	@Override
	public List<ActivityInfo> queryAll(ActivityInfo mroleId) {
		return activityDao.queryAll(mroleId);
	}

	@Override
	public int updateByClassId(ActivityInfo classIdInfo, UserInfoVO currentUser) {
		classIdInfo.setUpdateTime(new Date());
		classIdInfo.setUserId(currentUser.getUserId());
		return activityDao.updateByClassId(classIdInfo);
	}

}
